package xyz.naokeziteng.sort;

/**
 * 冒泡
 */
public class BubbleSort {

    public static int[] bubbleSort(int[] nums) {

        for (int i = 0; i < nums.length;) {
            int lastSwapIndex = 0;
            for (int j = 0; j < nums.length - i - 1; j++) {
                if (nums[j] > nums[j + 1]) {
                    int tmp = nums[j];
                    nums[j] = nums[j + 1];
                    nums[j + 1] = tmp;
                    lastSwapIndex = j + 1;
                }
            }
            i = nums.length - lastSwapIndex;
        }
        return nums;
    }

    public static void main(String[] args) {

        int[] nums = {5, 2, 7, 3, 8, 1, 9};
        bubbleSort(nums);
        for (int i = 0; i < nums.length; i++) {
            System.out.print(nums[i] + ",");
        }
    }
}
